// controllers/wordsetController.js
const WordSet = require("../models/wordsset");

const wordsetController = {
  createWordSet: (req, res) => {
    const { name, img, wordlist, user_id } = req.body;
    if (!name || user_id == null) {
      return res.status(200).send({
        message: "WordSet name and user ID are required",
        success: false,
      });
    }

    WordSet.create({ name, img, wordlist, user_id }, (error, results) => {
      if (error) {
        res.status(200).send({ message: error.message, success: false });
      } else {
        res.status(200).send({
          message: "WordSet created successfully",
          id: results.insertId,
          success: true,
        });
      }
    });
  },
  getUserWordSet: (req, res) => {
    try {
      // 从请求中获取page和limit参数
      const page = parseInt(req.body.page, 10) || 1; // 默认为第1页
      const limit = parseInt(req.body.limit, 10) || 10; // 默认每页显示10条记录
      if (!req.body.user_id) {
        return res.status(200).send({
          message: "WordSet name and user ID are required",
          success: false,
        });
      }
      WordSet.findAll(req.body.user_id, page, limit, (error, results) => {
        if (error) {
          res.status(200).send({ message: error.message, success: false });
        } else {
          // 返回分页数据
          res
            .status(200)
            .send({ data: results, page: page, limit: limit, success: true });
        }
      });
    } catch (error) {
      console.log(error);
    }
  },
  getWordSet: (req, res) => {
    const { id } = req.body;
    if (!id) {
      return res
        .status(200)
        .send({ message: "WordSet ID is required", success: false });
    }

    WordSet.findOne(id, (error, results) => {
      if (error) {
        res.status(200).send({ message: error.message, success: false });
      } else if (results.length === 0) {
        res.status(200).send({ message: "WordSet not found", success: false });
      } else {
        res.status(200).send({ data: results[0], success: true });
      }
    });
  },
  updateWordSet: (req, res) => {
    const { id, name, img, wordlist, user_id } = req.body;
    if (!id || !name || user_id == null) {
      return res.status(200).send({
        message: "WordSet ID, name, and user ID are required",
        success: false,
      });
    }

    WordSet.update({ id, name, img, wordlist, user_id }, (error, results) => {
      if (error) {
        res.status(200).send({ message: error.message, success: false });
      } else if (results.affectedRows === 0) {
        res.status(200).send({ message: "不存在", success: false });
      } else {
        res.status(200).send({ message: "更新成功", success: true });
      }
    });
  },
  deleteWordSet: (req, res) => {
    const { id, user_id } = req.body;
    if (!id || user_id == null) {
      return res.status(200).send({
        message: "WordSet ID and user ID are required",
        success: false,
      });
    }

    WordSet.delete(id, user_id, (error, results) => {
      if (error) {
        res.status(200).send({ message: error.message, success: false });
      } else if (results.affectedRows === 0) {
        res.status(200).send({ message: "WordSet not found", success: false });
      } else {
        res
          .status(200)
          .send({ message: "WordSet deleted successfully", success: true });
      }
    });
  },
  // 其他控制器方法...
};

module.exports = wordsetController;
